﻿using SMS.cntr;
using SMS.dao;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMS.util;
using System.Resources;

namespace SMS.view
{
    public partial class addBlock : Form
    {
        public addBlock()
        {
            InitializeComponent();
        }

      //=====================Quang=================================================================
        private string blockID;
        private string p;
        private void btnAddBlock_Click(object sender, EventArgs e)
        {
            
            String nameBlock = txtBlockName.Text;
            bool result = BlockCntr.Instance.createBlock(nameBlock);           
            if (result)
            {
                MessageBox.Show("Thêm khối thành công");
                bindGridViewBlock();
            }
   
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
        }

        private void btnUpdateBlock_Click(object sender, EventArgs e)
        {
            BlockCntr.Instance.update(txtBlockName.Text, blockID);
            bindGridViewBlock();

        }


        private void bindGridViewBlock()
        {

            gridViewBlock.Rows.Clear();
            try
            {
                DataTable table = BlockCntr.Instance.listAllBlockCounted();
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    gridViewBlock.Rows.Add(i + 1, table.Rows[i]["blo_name"].ToString(), table.Rows[i]["blo_id"].ToString(), table.Rows[i]["blo_counted"].ToString());
                    gridViewBlock.Rows[gridViewBlock.RowCount - 1].Tag = table.Rows[i];
                }
                gridViewBlock.ClearSelection();
            }
            catch (Exception ex) { }

        }

        private void gridViewBlock_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            for (int i = 0; i < gridViewBlock.SelectedRows.Count; i++)
            {
                int id = Int32.Parse(gridViewBlock.SelectedRows[i].Cells[2].Value.ToString());
                blockID = BlockDao.Instance.findById(id)[0].ToString();
                txtBlockName.Text = BlockDao.Instance.findById(id)[1].ToString();
                //DataTable dt = BlockDao.Instance.findBlockName();
                //DataRow[] dr = dt.Select("blo_name like '%" + txtBlockName + "%'");
                //DataTable dt = BlockDao.Instance.findBlockName(txtBlockName.ToString());
                //int count = dt.Rows.Count;
                //MessageBox.Show(count.ToString());
                
              

            }
        }

        private void addBlock_Load(object sender, EventArgs e)
        {
            bindGridViewBlock();
            changeLang(I18nUtil.vnLang);
        }

        public void changeLang(String lang)
        {
            I18nUtil.init(lang, new ResourceManager(I18nUtil.resourcePath, typeof(addBlock).Assembly));

            label1.Text = I18nUtil.getString("lblBlockName");
            btnAddBlock.Text = I18nUtil.getString("btnAdd");
            btnUpdateBlock.Text = I18nUtil.getString("btnUpdate");
            btnDeleteBlock.Text = I18nUtil.getString("btnDelete");
            gridViewBlock.Columns[0].HeaderText = I18nUtil.getString("no");
            gridViewBlock.Columns[1].HeaderText = I18nUtil.getString("blockName");
            gridViewBlock.Columns[3].HeaderText = I18nUtil.getString("blockCapacity");
        }

        private void btnDeleteBlock_Click(object sender, EventArgs e)
        {
            BlockCntr.Instance.delete(Int32.Parse(blockID));
            bindGridViewBlock();
        }

        //=====================================================================================


    }
}
